package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.sdk.app.PayTask;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.demo.PayResult;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Map;

public class MineAccountJoinMoney extends BaseActivity implements View.OnClickListener{

    private Button joinmoney;
    private EditText moneyno;
    private static final int SDK_PAY_FLAG = 1;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            PayResult payresult = new PayResult((Map<String, String>)msg.obj);
            String resultStatus = payresult.getResultStatus();
            if (resultStatus.equals("9000")){
                Toast.makeText(MineAccountJoinMoney.this, "支付成功", Toast.LENGTH_SHORT).show();
            }else if (resultStatus.equals("8000")){
                Toast.makeText(MineAccountJoinMoney.this, "支付处理中", Toast.LENGTH_SHORT).show();

            }else if (resultStatus.equals("4000")){
                Toast.makeText(MineAccountJoinMoney.this, "支付失败", Toast.LENGTH_SHORT).show();

            }else if (resultStatus.equals("5000")){
                Toast.makeText(MineAccountJoinMoney.this, "订单重复提交", Toast.LENGTH_SHORT).show();

            }else if (resultStatus.equals("6002")){
                Toast.makeText(MineAccountJoinMoney.this, "请检查你的网络是否启", Toast.LENGTH_SHORT).show();

            }else {
                Toast.makeText(MineAccountJoinMoney.this, "请稍等，支付结果获取中", Toast.LENGTH_SHORT).show();

            }
        }
    };

    private String money;
    private String data;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private ImageView back;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_account_join_money);
        initAccountView();
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");

    }

    private void initAccountView() {
        joinmoney = (Button) findViewById(R.id.joinmoney);
        moneyno = (EditText) findViewById(R.id.inputmoney);
        back = (ImageView) findViewById(R.id.accountjoin_back);
        backmain = (ImageView) findViewById(R.id.accountjoin_backmain);

        // 设置点击监听
        joinmoney.setOnClickListener(this);
        back.setOnClickListener(this);
        backmain.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.accountjoin_back:
                finish();
                break;
            case R.id.joinmoney:
                // 点击支付
                sendBook();

                break;
            case R.id.accountjoin_backmain:
                startActivity(new Intent(MineAccountJoinMoney.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
        }

    }
    private void sendBook() {
        money = String.valueOf(moneyno.getText());
        RequestParams params = new RequestParams(InternetAddressUtis.Url+"/alipay/recharge");
        params.setHeader("Content-Type","application/json");
        params.setHeader("token",token);
        params.setHeader("appId",appId);
        params.setHeader("userId",userId);
        final JSONObject j = new JSONObject();
        j.put("payType","01");
        j.put("total_amount", money);
        params.setBodyContent(j.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(final String result) {

                JSONObject obj = JSON.parseObject(result);
                data = obj.getString("data");
                final Runnable runnable = new Runnable() {
                    @Override
                    public void run() {
                        PayTask payTask = new PayTask(MineAccountJoinMoney.this);
                        Map<String, String> map = payTask.payV2(data, true);
                        Message msg = new Message();
                        msg.what = SDK_PAY_FLAG;
                        msg.obj = map;
                        mHandler.sendMessage(msg);
                    }
                };
                Thread payThread = new Thread(runnable);
                payThread.start();
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {

            }
        });




    }
}
